package com.wz.project.ddjqueue.controller;

import com.wz.common.utils.poi.ExcelUtil;
import com.wz.framework.aspectj.lang.annotation.Log;
import com.wz.framework.aspectj.lang.enums.BusinessType;
import com.wz.framework.web.controller.BaseController;
import com.wz.framework.web.domain.AjaxResult;
import com.wz.framework.web.page.TableDataInfo;
import com.wz.project.ddjqueue.domain.WmsDeviceExecutionQueue;
import com.wz.project.ddjqueue.service.IWmsDeviceExecutionQueueService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 堆垛机执行队列Controller
 * 
 * @author wld
 * @date 2023-04-06
 */
@Controller
@RequestMapping("/device/ddjqueue")
public class WmsDeviceExecutionQueueController extends BaseController
{
    private String prefix = "device/ddjqueue";

    @Autowired
    private IWmsDeviceExecutionQueueService wmsDeviceExecutionQueueService;

    @RequiresPermissions("device:ddjqueue:view")
    @GetMapping()
    public String ddjqueue()
    {
        return prefix + "/ddjqueue";
    }

    /**
     * 查询堆垛机执行队列列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WmsDeviceExecutionQueue wmsDeviceExecutionQueue)
    {
        startPage();
        List<WmsDeviceExecutionQueue> list = wmsDeviceExecutionQueueService.selectWmsDeviceExecutionQueueList(wmsDeviceExecutionQueue);
        return getDataTable(list);
    }

    /**
     * 导出堆垛机执行队列列表
     */
    @Log(title = "堆垛机执行队列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WmsDeviceExecutionQueue wmsDeviceExecutionQueue)
    {
        List<WmsDeviceExecutionQueue> list = wmsDeviceExecutionQueueService.selectWmsDeviceExecutionQueueList(wmsDeviceExecutionQueue);
        ExcelUtil<WmsDeviceExecutionQueue> util = new ExcelUtil<WmsDeviceExecutionQueue>(WmsDeviceExecutionQueue.class);
        return util.exportExcel(list, "堆垛机执行队列数据");
    }

    /**
     * 新增堆垛机执行队列
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存堆垛机执行队列
     */
    @Log(title = "堆垛机执行队列", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WmsDeviceExecutionQueue wmsDeviceExecutionQueue)
    {
        return toAjax(wmsDeviceExecutionQueueService.insertWmsDeviceExecutionQueue(wmsDeviceExecutionQueue));
    }

    /**
     * 修改堆垛机执行队列
     */
    @GetMapping("/edit/{rwh}")
    public String edit(@PathVariable("rwh") Long rwh, ModelMap mmap)
    {
        WmsDeviceExecutionQueue wmsDeviceExecutionQueue = wmsDeviceExecutionQueueService.selectWmsDeviceExecutionQueueByRwh(rwh);
        mmap.put("wmsDeviceExecutionQueue", wmsDeviceExecutionQueue);
        return prefix + "/edit";
    }

    /**
     * 修改保存堆垛机执行队列
     */
    @Log(title = "堆垛机执行队列", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WmsDeviceExecutionQueue wmsDeviceExecutionQueue)
    {
        return toAjax(wmsDeviceExecutionQueueService.updateWmsDeviceExecutionQueue(wmsDeviceExecutionQueue));
    }

    /**
     * 删除堆垛机执行队列
     */
    @Log(title = "堆垛机执行队列", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wmsDeviceExecutionQueueService.deleteWmsDeviceExecutionQueueByRwhs(ids));
    }

    /**
     * 重新下发堆垛机执行队列
     */
    @Log(title = "堆垛机执行队列", businessType = BusinessType.UPDATE)
    @RequestMapping("/reloadTask")
    @ResponseBody
    public AjaxResult reloadTask(WmsDeviceExecutionQueue queue){
        int sa = wmsDeviceExecutionQueueService.reloadTask(queue);
        if(sa == 1){
            return AjaxResult.success("重新下发成功！");
        }else{
            return AjaxResult.error("只有正执行的队列才可以重新下发！");
        }
    }

    /**
     * 删除堆垛机执行队列
     */
    @Log(title = "堆垛机执行队列", businessType = BusinessType.DELETE)
    @RequestMapping("/deleteTask")
    @ResponseBody
    public AjaxResult deleteTask(WmsDeviceExecutionQueue queue){
        int sa = wmsDeviceExecutionQueueService.deleteTask(queue);
        if(sa == 1){
            return AjaxResult.success("删除成功！");
        }else{
            return AjaxResult.error("只有正执行的队列才可以删除！");
        }
    }
}
